package com.atguigu.controller;


import com.atguigu.entity.PlatformPropertyKey;
import com.atguigu.entity.PlatformPropertyValue;
import com.atguigu.result.RetVal;
import com.atguigu.service.PlatformPropertyKeyService;
import com.atguigu.service.PlatformPropertyValueService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 属性表 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/product")
@Api(tags = "平台属性接口")
public class PlatformPropertyKeyController {
    @Autowired
    private PlatformPropertyKeyService platformPropertyKeyService;
    @Autowired
    private PlatformPropertyValueService platformPropertyValueService;

   //查询平台属性列表
    //http://127.0.0.1/product/getPlatformPropertyByCategoryId/2/0/0
    @ApiOperation("查询平台属性列表")
    @GetMapping("getPlatformPropertyByCategoryId/{baseCategory1}/{baseCategory2}/{baseCategory3}")
    public RetVal getPlatformPropertyByCategoryId(@PathVariable("baseCategory1") Long baseCategory1,
                                                  @PathVariable("baseCategory2") Long baseCategory2,
                                                  @PathVariable("baseCategory3") Long baseCategory3) {
        List<PlatformPropertyKey> platformPropertyKeyList = platformPropertyKeyService.getPlatformPropertyByCategoryId(baseCategory1, baseCategory2, baseCategory3);
        return RetVal.ok(platformPropertyKeyList);
    }

    //查询平台属性 值  列表
    //http://127.0.0.1:8000/product/getPropertyValueByPropertyKeyId/4
    @ApiOperation("根据平台属性查对应的平台属性值")
    @GetMapping("getPropertyValueByPropertyKeyId/{propertyKeyId}")
    public RetVal getPropertyValueByPropertyKeyId(@PathVariable("propertyKeyId") Long propertyKeyId) {
        QueryWrapper<PlatformPropertyValue> wrapper = new QueryWrapper<>();
        wrapper.eq("property_key_id", propertyKeyId);
        List<PlatformPropertyValue> platformPropertyValues = platformPropertyValueService.list(wrapper);
        return RetVal.ok(platformPropertyValues);
    }
    //http://127.0.0.1/product/savePlatformProperty
    @PostMapping("savePlatformProperty")
    @ApiOperation("保存平台属性")
    public RetVal savePlatformProperty(@RequestBody PlatformPropertyKey platformPropertyKey) {
        Boolean flag = platformPropertyKeyService.savePlatformProperty(platformPropertyKey);
        if (flag) {
            return RetVal.ok();
        } else {
            return RetVal.fail();
        }
    }
    //d.根据商品的skuId查询商品的平台属性
    @GetMapping("selectProductKeyFromSkuId/{skuId}")
    @ApiOperation("根据商品的skuId查询商品的平台属性")
    public List<PlatformPropertyKey> selectProductKeyFromSkuId(@PathVariable Long skuId) {
        return platformPropertyKeyService.getPlatformPropertyBySkuId(skuId);
    }

}

